//
//  TXViewController.swift
//  MultilevelDirectory
//
//  Created by Yang on 2024/6/18.
//

import UIKit

class TXViewController: UIViewController {
    
    @IBOutlet weak var mainTableView: UITableView!
    @IBOutlet weak var selBtn: UIButton!
    @IBOutlet weak var goBackBtn: UIButton!
    private var selIndex = 0
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.mainTableView.delegate = self
        self.mainTableView.dataSource = self
        self.mainTableView.register(UINib(nibName: "TXTechnologyMenuListCell", bundle: nil),
                                    forCellReuseIdentifier: "TXTechnologyMenuListCell")
        self.goBackBtn.layer.borderColor = UIColor.blue.cgColor
        self.goBackBtn.layer.borderWidth = 1
        self.mainTableView.rowHeight = UITableView.automaticDimension
        self.getListData()
    }
    
    private lazy var viewModel: TXTechnologyMenuListViewModel = {
        let viewModel = TXTechnologyMenuListViewModel()
        viewModel.delegate = self
        return viewModel
    }()
    
    var Id = ""
    @IBAction func selBtnClick(_ sender: UIButton) {
        guard let model = self.viewModel.selModel else { return }
        if model.children.count > 0 {
            viewModel.listArray = model.children
        }
        self.mainTableView.reloadData()
        if let model = self.viewModel.selModel, model.children.count == 0 {
            debugPrint("ssssssssssssssssssssssss-  ",model.label, model.id)
        } else {
            selIndex += 1
            self.changeSelBtnState()
            self.goBackBtn.isHidden = false
        }
    }
    
    @IBAction func loadBtnClick(_ sender: UIButton) {
        self.getListData()
    }
    @IBAction func goBackBtnClick(_ sender: UIButton) {
        // 当前级别列表全部取消选中
        viewModel.listArray.forEach { $0.isSelect = false }
        if selIndex > 1 {
            Id = viewModel.selModel?.pid ?? ""
            let array = self.viewModel.findDirectory(in: self.viewModel.selList, withName: Id)
            let model = array.first
            let array1 = self.viewModel.findDirectory(in: self.viewModel.selList, withName: model?.pid ?? "")
            self.viewModel.selModel = array1.filter({$0.isSelect == true}).first
            viewModel.listArray = self.viewModel.selModel?.children ?? []
        } else  {
            guard let selModel = viewModel.selModel else { return }
            let ids = selModel.pid != "0" ? selModel.pid : selModel.id
            self.viewModel.selList.forEach { item in
                item.isSelect = false
                if item.id == ids {
                      item.isSelect = true
                }
            }
            self.viewModel.listArray = self.viewModel.selList
            self.viewModel.selModel = self.viewModel.selList.filter({$0.isSelect == true}).first
        }
        selIndex -= 1
        if self.selIndex == 0 {
            self.goBackBtn.isHidden = true
        }
        self.mainTableView.reloadData()
        changeSelBtnState()
    }
    
    private func changeSelBtnState() {
        if viewModel.listArray.filter({ $0.isSelect == true }).count > 0 {
            selBtn.backgroundColor = UIColor.blue
            selBtn.isEnabled = true
        } else {
            selBtn.backgroundColor = UIColor.blue.withAlphaComponent(0.6)
            selBtn.isEnabled = false
        }
    }
}

// MARK: - ***************** 网络请求Api *****************
extension TXViewController {
    private func getListData() {
        viewModel.setData()
    }
}

// MARK: - ***************** TXTechnologyMenuListViewModelDelegate *****************
extension TXViewController: TXTechnologyMenuListViewModelDelegate {
    func reloadData() {
        self.mainTableView.reloadData()
        changeSelBtnState()
    }
}

// MARK: - ***************** UITableViewDelegate, UITableViewDataSource *****************
extension TXViewController: UITableViewDelegate, UITableViewDataSource {
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return viewModel.listArray.count
    }
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        guard let cell = tableView.dequeueReusableCell(withIdentifier: "TXTechnologyMenuListCell") as? TXTechnologyMenuListCell  else {
            fatalError("dequeue PlanCell Error")
        }
        let model = self.viewModel.listArray[indexPath.row]
        cell.setModel(model: model)
        return cell
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        tableView.deselectRow(at: indexPath, animated: true)
        let model = self.viewModel.listArray[indexPath.row]
        self.viewModel.listArray.forEach { item in
            item.isSelect = false
            if item.id == model.id {
                item.isSelect = true
            }
        }
        Id = model.pid
        self.viewModel.selModel = model
        changeSelBtnState()
        self.mainTableView.reloadData()
    }
}
